//
//  TTUDIDManager.m
//  TTLoveCar
//
//  Created by 王洋 on 16/5/11.
//  Copyright © 2016年 王洋. All rights reserved.
//

#import "TTUDIDManager.h"
#import <AdSupport/AdSupport.h>
#import "SAMKeychain.h"

@implementation TTUDIDManager

+ (NSString *)getUniqueDeviceID
{
    NSString *deviceID = [SAMKeychain passwordForService:@"tts_ios" account:@"app_user"];
    if (deviceID) {
        return deviceID;
    }

    //IDFA 广告唯一标识符
    NSString *IDFA_NEW = nil;
    if ([[ASIdentifierManager sharedManager] isAdvertisingTrackingEnabled]) {
        IDFA_NEW = [[ASIdentifierManager sharedManager].advertisingIdentifier UUIDString];
    }else {
        IDFA_NEW = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
    }
    IDFA_NEW = [IDFA_NEW stringByReplacingOccurrencesOfString:@"-" withString:@""];
    IDFA_NEW = [IDFA_NEW lowercaseString];
    
    if (IDFA_NEW && ![IDFA_NEW isEqualToString:@""]) {
        deviceID = [IDFA_NEW copy];
    } else {
        deviceID = @"NULL";
    }
    
    NSRange range = {[deviceID length]-12, 12};
    NSString *udid = [deviceID substringWithRange:range];
    udid = [NSString stringWithFormat:@"%@I",udid];
    deviceID = udid;
    
    [SAMKeychain setPassword:deviceID forService:@"tts_ios" account:@"app_user"];
    
    return deviceID;
}


@end
